//
//  AppDelegate.m
//  RedballBar
//
//  Created by Faith on 2022/12/23.
//

#import "AppDelegate.h"
#import "Controllers/Home/HomeController.h"
#import "Controllers/Score/ScoreController.h"
#import "Controllers/Live/LiveController.h"
#import "Controllers/Material/MaterialController.h"
#import "Controllers/User/UserController.h"

@interface AppDelegate()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 禁用约束警告
    [[NSUserDefaults standardUserDefaults] setValue:@(NO) forKey:@"_UIConstraintBasedLayoutLogUnsatisfiable"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark]; // 设置样式（暗色或亮色）
    [SVProgressHUD setMinimumDismissTimeInterval:1.0]; // 设置最小消失时间
    [SVProgressHUD setMaximumDismissTimeInterval:5.0]; // 设置最大消失时间
    
    // 初始化根控制器
    [self initRootController];
    
    return YES;
}

/**
 * 初始化根控制器
 */
- (void)initRootController {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    self.window.rootViewController = self.rootViewController;
    // 禁用深色模式
    if (@available(iOS 13.0, *)) {
        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    [self.window makeKeyAndVisible];
    
}

/**
 * 懒加载根控制器
 */
- (UINavigationController *)rootViewController {
    if(!_rootViewController) {
        HomeController *homeController = [[HomeController alloc] init];
        homeController.tabBarItem.title = @"首页";
        homeController.tabBarItem.image = [UIImage imageNamed:@"icon-home-unselected@3x"];
        homeController.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon-home-selected@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UINavigationController *navHomeController = [[UINavigationController alloc] initWithRootViewController:homeController];
        navHomeController.navigationBar.hidden = YES;

        ScoreController *scoreController = [[ScoreController alloc] init];
        scoreController.tabBarItem.title = @"发现";
        scoreController.tabBarItem.image = [UIImage imageNamed:@"icon-discover-unselected@3x"];
        scoreController.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon-discover-selected@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UINavigationController *navScoreController = [[UINavigationController alloc] initWithRootViewController:scoreController];
        navScoreController.navigationBar.hidden = YES;
        
        LiveController *liveController = [[LiveController alloc] init];
        liveController.tabBarItem.title = @"消息";
        liveController.tabBarItem.image = [UIImage imageNamed:@"icon-message-unselected@3x"];
        liveController.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon-message-selected@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UINavigationController *navLiveController = [[UINavigationController alloc] initWithRootViewController:liveController];
        navLiveController.navigationBar.hidden = YES;
        
        MaterialController *materialController = [[MaterialController alloc] init];
        materialController.tabBarItem.title = @"购物车";
        materialController.tabBarItem.image = [UIImage imageNamed:@"icon-shopcart-unselected@3x"];
        materialController.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon-shopcart-selected@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UINavigationController *navMaterialController = [[UINavigationController alloc] initWithRootViewController:materialController];
        navMaterialController.navigationBar.hidden = YES;
        
        UserController *userController = [[UserController alloc] init];
        userController.tabBarItem.title = @"我的";
        userController.tabBarItem.image = [UIImage imageNamed:@"icon-my-unselected@3x"];
        userController.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon-my-selected@3x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UINavigationController *navUserController = [[UINavigationController alloc] initWithRootViewController:userController];
        navUserController.navigationBar.hidden = YES;
        navUserController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
        navUserController.interactivePopGestureRecognizer.enabled = YES;

        UITabBar *tabBar = [[UITabBar alloc] init];
        tabBar.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        tabBar.tintColor = [UIColor colorWithHexString:@"#DD3B45"];
        
        [self.tabbarController setValue:tabBar forKey:@"tabBar"];
        [self.tabbarController setViewControllers:@[homeController, scoreController, liveController, materialController, userController]];
        [self.tabbarController setSelectedIndex:4];
        
        _rootViewController = [[UINavigationController alloc] initWithRootViewController:self.tabbarController];
        _rootViewController.navigationBarHidden = YES;
    }
    return _rootViewController;
}

- (UITabBarController *)tabbarController {
    if(!_tabbarController) {
        _tabbarController = [[UITabBarController alloc] init];
    }
    return _tabbarController;
}

/**
 * 刷新根控制器
 */
- (void)refreshRootViewController {
    // 移除根控制器
    [self.window.rootViewController removeFromParentViewController];
    self.window.rootViewController = nil;
    _rootViewController = nil;
    // 重新初始化根控制器
    self.window.rootViewController = _rootViewController;
    [self.window makeKeyAndVisible];
}


@end
